using FoundationLib.AddIns.Constraints.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FoundationLib.AddIns.Constraints.Visitors
{
	/// <summary>
	/// Represents a visitor which evaluates OCL collection quantifier statements
	/// </summary>
	public class CollectionLoopVisitor : RegexVisitor
	{
		private const string PATTERN = @"([^- ]+)->([^\(]+)\(([^\|]+)\|([^\)]+)\)";

		/// <summary>
		/// Creates a new collection loop visitor
		/// </summary>
		public CollectionLoopVisitor()
			: base(PATTERN)
		{
		}

		/// <summary>
		/// Evaluates regular expession matches
		/// </summary>
		/// <returns>The result of the evaluation</returns>
		/// <param name="match">The regular expression match</param>
		protected override string Evaluate(Match match)
		{
			string loopType = null;
			if (match.Groups[2].Value == "exists")
				loopType = "Exists";
			else if (match.Groups[2].Value == "forAll")
				loopType = "ForAll";

			string format = FormatManager.GetFormat(loopType);

			if (!String.IsNullOrWhiteSpace(loopType))
				return String.Format(format, match.Groups[1].Value + ", " + match.Groups[3].Value.Trim() + " => " + 
					match.Groups[4].Value.Trim());
			else
				return match.Value;
		}
	}
}
